package capricornus.utils
{
	import capricornus.core.StageManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class HitManager
	{
		private static const ORIGINAL_POINT:Point = new Point();
		public function HitManager()
		{
		}
		
		public static function hitTestObject( dis1:DisplayObject, dis2:DisplayObject ):Boolean
		{
			var rect1:Rectangle = dis1.getBounds( StageManager.stage );
			var rect2:Rectangle = dis2.getBounds( StageManager.stage );
			if( !rect1.intersects( rect2 ) )
			{
				return false;
			}
			var rect:Rectangle = rect1.intersection( rect2 );
			
			var bd1:BitmapData = new BitmapData( rect.width, rect.height, true, 0 );
			var pt:Point = new Point( rect.x, rect.y );
			pt = dis1.globalToLocal( pt );
			var mt:Matrix = new Matrix();
			mt.tx = -pt.x;
			mt.ty = -pt.y;
			bd1.draw( dis1, mt );
			
			var bd2:BitmapData = new BitmapData( rect.width, rect.height, true, 0 );
			pt = new Point( rect.x, rect.y );
			pt = dis2.globalToLocal( pt );
			mt.tx = -pt.x;
			mt.ty = -pt.y;
			bd2.draw( dis2, mt );
			
			var b:Boolean = bd1.hitTest( ORIGINAL_POINT, 100, bd2, ORIGINAL_POINT, 100 );
			bd1.dispose();
			bd2.dispose();
			
			return b;
		}
	}
}